// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
*@describe:
*@author wfl19/Kristas
*@date 2021/12/18
 */

package gorm_plugin

import (
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/data/datasource"
	"gorm.io/gorm"
)

func NewDB(datasource, table string) *DB {
	return &DB{datasource: datasource, table: table}
}

type DB struct {
	types.Component
	datasource string
	table      string
	*gorm.DB
}

func (d *DB) Bean() string {
	return "gorm_db:" + d.datasource + "@" + d.table
}

func (d *DB) DataSource() string {
	return "application.datasource"
}

func (d *DB) SetDB(db datasource.DB) {
	d.DB = MustBindDB(db)
}
